package service;
import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import common.kit.DateKits;
import common.kit.PageSql;
import model.SampleRecord;
import model.Staff;
import model.Station;

public class SampleRecordService extends BaseService {

	public Page<Record> getPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tsr.id,tsr.createTime,tsr.recorder,tsr.projectName,tsr.unit,tsr.startTime,tsr.endTime,tsr.sampleSolution,tsr.instrumentValue,tsr.error,tsr.isNormal,tsr.instrumentName,tsr.instrumentStartTime,tsr.instrumentEndTime,tsr.paramType,tsr.result,tsr.instrumentIsNormal,ts.name,tsr1.name AS servicerName,tc.name AS companyName1 " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_sample_record tsr ") ;
	    pageSql.join(" t_station ts " , " ts.id = tsr.stationId ") ;
	    pageSql.join(" t_servicer tsr1 " , " tsr1.id = tsr.serviserId ") ;
	    pageSql.join(" t_company tc " , " tc.id = ts.companyId ") ;
		pageSql.setNotLessThanCondition("tsr.createTime","tsr.createTime.start") ; //创建时间
		pageSql.setNotMoreThanCondition("tsr.createTime","tsr.createTime.end") ; //创建时间
		pageSql.setEqualCondition("tsr.stationId") ; //站点	 
		pageSql.setLikeCondition("ts.name") ; //点位名称	 
		pageSql.setLikeCondition("tsr1.name") ; //运维公司	 
		pageSql.setLikeCondition("tc.name") ; //企业名称	 
	    pageSql.orderBy(" tsr.createTime DESC ");
		return pageSql.paginate( pageNumber , pageSize) ;
	}

	public Page<Record> getApiPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tsr.id,tsr.createTime,tsr.recorder,tsr.projectName,tsr.unit,tsr.startTime,tsr.endTime,tsr.sampleSolution,tsr.instrumentValue,tsr.error,tsr.isNormal,tsr.instrumentName,tsr.instrumentStartTime,tsr.instrumentEndTime,tsr.paramType,tsr.result,tsr.instrumentIsNormal,ts.name " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_sample_record tsr ") ;
	    pageSql.join(" t_servicer ts " , " ts.id = tsr.serviserId ") ;
		pageSql.setEqualCondition("tsr.stationId") ; //站点	 
		pageSql.setNotLessThanCondition("tsr.createTime", "startTime");
		pageSql.setNotMoreThanCondition("tsr.createTime", "endTime");
	    pageSql.orderBy(" tsr.createTime DESC ");
		Page<Record> page = pageSql.paginate( pageNumber , pageSize) ;
		List<Record> list = page.getList();
		for (Record record : list) {
			record.set("startTime", DateKits.toStr( record.getDate("startTime") , DateKits.MINITE ));
			record.set("endTime", DateKits.toStr( record.getDate("endTime") , DateKits.MINITE ));
			record.set("instrumentStartTime", DateKits.toStr( record.getDate("instrumentStartTime") , DateKits.MINITE ));
			record.set("instrumentEndTime", DateKits.toStr( record.getDate("instrumentEndTime") , DateKits.MINITE ));
		}
		return page ;
	}

	public Ret switchIsNormal(Long id) {
		SampleRecord sampleRecord = SampleRecord.dao.findById(id);
		if (null == sampleRecord) {
			return fail();
		}
		sampleRecord.setIsNormal(!sampleRecord.getIsNormal());
		return sampleRecord.update() ? ok() : fail() ;
	}

	public Ret switchInstrumentIsNormal(Long id) {
		SampleRecord sampleRecord = SampleRecord.dao.findById(id);
		if (null == sampleRecord) {
			return fail();
		}
		sampleRecord.setInstrumentIsNormal(!sampleRecord.getInstrumentIsNormal());
		return sampleRecord.update() ? ok() : fail() ;
	}

	public Ret save(SampleRecord sampleRecord , Station station , Staff staff) {
		sampleRecord.setServiserId(staff.getServicerId());
		sampleRecord.setRecorder(staff.getName());
		sampleRecord.setStationId(station.getId());
		sampleRecord.save();
		//保存维护记录
		new MaintainRecordsService().saveQutity(sampleRecord, station, staff);
	    return ok() ;
	}
}
